﻿Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class lbox
    Inherits ListBox

    Public Sub New()
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
        Me.ItemHeight = 40
        Me.IntegralHeight = False
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        For i As Integer = Me.Items.Count - 1 To 0 Step -1
            If Me.SelectedIndex <> i Then
                Dim rct As Rectangle = Me.GetItemRectangle(i)

                g.FillRectangle(Brushes.White, rct)

                Dim p() As String = Split(Me.Items(i), "| ")

                g.DrawString(p(0), New Font("Segoe UI", 9), Brushes.Black, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Near})

                g.DrawString(p(1), New Font("Segoe UI", 9), Brushes.DimGray, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Far})

                Dim p1 As New Point(rct.X, rct.Y + rct.Height - 1)
                Dim p2 As New Point(rct.X + rct.Width, rct.Y + rct.Height - 1)

                g.DrawLine(Pens.Silver, p1, p2)

            End If
        Next

        If Me.SelectedIndex <> -1 Then
            Dim rct As Rectangle = Me.GetItemRectangle(Me.SelectedIndex)

            g.FillRectangle(Brushes.WhiteSmoke, rct)

            Dim p() As String = Split(Me.Items(Me.SelectedIndex), "| ")

            g.DrawString(p(0), New Font("Segoe UI", 9, FontStyle.Bold), Brushes.Black, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Near})

            g.DrawString(p(1), New Font("Segoe UI", 9), Brushes.DimGray, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Far})

            Dim p1 As New Point(rct.X, rct.Y + rct.Height - 1)
            Dim p2 As New Point(rct.X + rct.Width, rct.Y + rct.Height - 1)

            g.DrawLine(Pens.Silver, p1, p2)
        End If
    End Sub

    Private Sub lbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Me.SelectedIndexChanged
        Me.Invalidate()

    End Sub
End Class
